package com.partner.boot.service.impl;

import cn.hutool.core.util.StrUtil;
import com.partner.boot.common.enums.AuthEnum;
import com.partner.boot.entity.Permission;
import com.partner.boot.mapper.PermissionMapper;
import com.partner.boot.service.IPermissionService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import io.swagger.models.auth.In;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.*;
import java.util.stream.Collectors;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 菜牙买菜
 * @since 2023-01-17
 */
@Service
public class PermissionServiceImpl extends ServiceImpl<PermissionMapper, Permission> implements IPermissionService {

//    private static final String template = "{auth}.add,{authName}添加," +
//            "{auth}.edit,{authName}编辑," +
//            "{auth}.delete,{authName}删除," +
//            "{auth}.list,查询所有{authName}";

    @Resource
    private PermissionMapper permissionMapper;

    @Override
    public List<Permission> tree() {
        List<Permission> list = list();
        return childrenTree(null, list); // 从第一级开始往下递归获取树
    }

    @Override
    public void savePermission(Permission permission) {
        Integer type = permission.getType(); // 获取添加菜单的类型
//        if (type == 2) {
//            permissionMapper.insert(permission);
//            String name = StrUtil.removeSuffix(permission.getName(),"管理"); // 处理去新添加菜单中的 管理 两字
//            String componentName = permission.getComponentName(); // 添加菜单的的组件名称 user
//            Integer pid = permission.getId(); // 权限按钮的父级菜单
//            HashMap<String,String> map = new HashMap<>();
//            ArrayList<String> authList = new ArrayList<>(); // user.add
//            ArrayList<String> authNameList = new ArrayList<>(); // 用户添加
//            map.put(".add","添加");
//            map.put(".edit","编辑");
//            map.put(".list","查询所有");
//            map.put(".delete","删除");
//            map.forEach((key,value) -> {
//                authList.add(componentName + key); //
//                authNameList.add(name + value);
//            });
//            for (int i = 0; i < 4; i++) {
//                Permission permission1 = new Permission();
//                permission1.setPid(pid);
//                permission1.setName(authNameList.get(i));
//                permission1.setAuth(authList.get(i));
//                permission1.setType(3);
//                permissionMapper.insert(permission1);
//            }
//        } else {
//            permissionMapper.insert(permission);
//        }
        permissionMapper.insert(permission);
    }

    public List<Permission> childrenTree(Integer pid, List<Permission> allData) {
        ArrayList<Permission> list = new ArrayList<>();
        for (Permission permission : allData) {
            if (Objects.equals(permission.getPid(), pid)) { // null 一级
                list.add(permission);
                List<Permission> childrenTree = childrenTree(permission.getId(), allData); // 递归调用
                permission.setChildren(childrenTree);
            }
        }
        return list.stream().sorted(Comparator.comparing(Permission::getSort).reversed()).collect(Collectors.toList());
    }

}
